package ink.onei.production.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.onei.production.entity.Dept;
import ink.onei.production.entity.Staff;
import ink.onei.production.service.IDepartmentService;
import ink.onei.production.vo.Result;
import ink.onei.production.vo.StaffAndDeptVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author: dang
 * @Description: TODO
 * @Date: 2023/12/27 14:22 星期三
 */
@Controller
@RequestMapping("/dept")
public class DepartmentController {

    @Autowired
    IDepartmentService departmentService;

    //查询所有部门
    @RequestMapping("/deptList")
    @ResponseBody
    public Result<Dept> getDeptAll(String page, String limit) {
        int i = Integer.parseInt(page);
        int j = Integer.parseInt(limit);
        PageHelper.startPage(i, j);
        Result<Dept> result = new Result<>();
        List<Dept> deptList = departmentService.getDeptAll();
        result.setCode(200);
        PageInfo<Dept> deptPageInfo = PageInfo.of(deptList);

        result.setTotal(deptPageInfo.getTotal());
        result.setMsg("请求成功");
        result.setData(deptList);
        return result;
    }



    //搜索
    @RequestMapping("/deptSearch")
    @ResponseBody
    public Result<Dept> deptSearch(String page, String limit, String id, String deptName) {
        Dept dept = new Dept();
        if (id == null || id == "") {
            dept.setId(0);
        } else {
            dept.setId(Integer.parseInt(id));
        }

        dept.setDeptName(deptName);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        Result<Dept> result = new Result<>();
        List<Dept> deptList = departmentService.deptSearch(dept);
        result.setCode(200);
//        PageInfo<Dept> deptPageInfo = PageInfo.of(deptList);
        result.setTotal(deptList.size());
//        result.setTotal(deptPageInfo.getTotal());
        result.setMsg("请求成功");
        result.setData(deptList);
        return result;
    }

    //添加部门
    @RequestMapping("/deptAdd")
    @ResponseBody
    public Result deptAdd(@RequestBody Dept dept) {
        Result result = new Result();

        Integer id = dept.getId();
        Dept deptObj = departmentService.getDeptById(id);
        if (deptObj == null) {
            result.setCode(200);
            Integer res = departmentService.addDept(dept);

            result.setMsg("添加成功");
        } else {
            result.setCode(400);
            result.setMsg("添加失败:该部门id已存在请检查!!!");
        }
        return result;
    }

    //删除部门
    @RequestMapping("/deptDel")
    @ResponseBody
    public Result deptDel(@RequestBody Dept dept) {
        Result result = new Result();

        Integer id = dept.getId();
        Integer res = departmentService.DelDeptById(id);
        if (res != 0) {
            result.setCode(200);
            result.setMsg("删除成功");
        } else {
            result.setCode(400);
            result.setMsg("删除失败");
        }
        return result;
    }

    //批量删除部门
    @RequestMapping("/deptBatch")
    @ResponseBody
    public Result<Dept> deptBatch(@RequestBody List<Dept> deptList) {
        Result result = new Result();
        Integer i = departmentService.deptBatch(deptList);


        return result;
    }

    //编辑部门
    @RequestMapping("/deptEdit")
    @ResponseBody
    public Result deptEdit(@RequestBody Dept dept) {
        Result result = new Result();
        Integer res = departmentService.EditDeptById(dept);
        if (res != null) {
            result.setCode(200);
            result.setMsg("删除成功");
        } else {
            result.setCode(400);
            result.setMsg("删除失败");
        }
        return result;
    }
    @RequestMapping("/staffEdit")
    @ResponseBody
    public Result staffEdit(@RequestBody Staff staff) {
        Result result = new Result();
        Integer res = departmentService.EditStaffById(staff);
        if (res != null) {
            result.setCode(200);
            result.setMsg("删除成功");
        } else {
            result.setCode(400);
            result.setMsg("删除失败");
        }
        return result;
    }

    //查询所有职工
    @RequestMapping("/staffList")
    @ResponseBody
    public Result getStaffAll(String page, String limit) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        Result result = new Result();
        List<StaffAndDeptVO> staffAllList = departmentService.getStaffAll();
        result.setCode(200);
        result.setMsg("请求成功");
        result.setData(staffAllList);
        return result;
    }


}
